package com.tzstcl.sys.user.service.impl;

import com.tzstcl.base.service.impl.BaseServiceImpl;
import com.tzstcl.sys.user.mapper.SysRoleMapper;
import com.tzstcl.sys.user.mapper.SysRolePermissionMapper;
import com.tzstcl.sys.user.model.SysRole;
import com.tzstcl.sys.user.model.SysRolePermission;
import com.tzstcl.sys.user.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 公司：天筑科技股份有限公司
 * 作者：guohy
 * 日期：2019年11月08日
 * 说明：角色表ServiceImpl
 */
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 批量增加角色表
     *
     * @param sysRoleList
     * @return Integer 插入的记录数
     * @author guohy
     * @date 2019年11月08日
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public Integer insertBatch(List<SysRole> sysRoleList) {
        return this.mapper.insertBatch(sysRoleList);
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void save(SysRole sysRole) {
        if (null == sysRole.getId()) {
            sysRole.preInit();
            sysRole.setUseable("0");
            sysRoleMapper.insert(sysRole);
            if (null != sysRole.getPermissionIds() && sysRole.getPermissionIds().size() > 0) {
                insertPermissionIds(sysRole);
            }
        }
        else {
            sysRole.preInit();
            sysRoleMapper.update(sysRole);
            //修改权限删除原有权限
            if (null != sysRole.getPermissionIds() && sysRole.getPermissionIds().size() > 0) {
                sysRolePermissionMapper.delete(sysRole.getId());
                insertPermissionIds(sysRole);
            }
        }
    }

    /**
     * @description 新增权限
     * @author guohy
     * @date 2019/11/9-10:11
     * @param sysRole
     * @return void
     */
    private void insertPermissionIds(SysRole sysRole) {
        List<SysRolePermission> list = new ArrayList<>();
        SysRolePermission rolePermission = null;
        for (Long id : sysRole.getPermissionIds()) {
            rolePermission = new SysRolePermission();
            rolePermission.setRoleId(sysRole.getId());
            rolePermission.setPermissionId(id);
            list.add(rolePermission);
        }
        if (list.size() > 0) {
            sysRolePermissionMapper.batchInsert(list);
        }
    }

    /**
     * @description 删除单个
     * @author guohy
     * @date 2019/11/9-11:30
     * @param sysRole
     * @return java.lang.Integer
     */
    @Transactional(readOnly = false)
    public Integer deleteOne(SysRole sysRole) {
        // 删除角色
        sysRoleMapper.delete(sysRole.getId());
        //删除角色的权限
        sysRolePermissionMapper.delete(sysRole.getId());
        return 1;
    }

}